package com.running.base.run.api;


import com.running.base.run.domain.User;
import com.running.base.run.exception.enums.CommonEnum;
import com.running.base.run.params.base.ResultBody;
import com.running.base.run.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by Daniel on 24/02/2017.
 */
@RestController
@RequestMapping("/api/monitor")
public class AliveController {

    private static final Logger logger = LoggerFactory.getLogger(AliveController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/alive")
    public ResultBody alive() throws InterruptedException {
        logger.info("alive ....");
        return ResultBody.success();
    }

    /**
     * 获取所有用户列表
     */
    @GetMapping("/users")
    public ResultBody getUsers() {
        logger.info("开始处理获取所有用户列表请求");
        try {
            List<User> users = userService.findAll();
            ResultBody successResult = ResultBody.success(users);
            logger.info("获取所有用户列表成功，共{}条记录，返回结果: {}", users.size(), successResult);
            return successResult;
        } catch (Exception e) {
            logger.error("获取所有用户列表过程中发生异常", e);
            return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
